package com.tencent.cloud.iov.util.log;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.tencent.cloud.iov.util.DateTimeUtils;
import com.tencent.mars.xlog.Log;
import com.tencent.mars.xlog.Xlog;

/**
 * 日志管理类
 *
 * @author showxiao
 * @date 2018/3/26
 */

public class LogManager {

    /**
     * 加载动态库：需要确保在使用日志模块前调用
     */
    static {
        System.loadLibrary("stlport_shared");
        System.loadLibrary("marsxlog");
    }

    private static final String LOG_FILE_PREFIX = "TXCar";
    private static final String LOG_FILE_POSTFIX = ".xlog";

    private static volatile boolean sHasInitialized = false;

    /**
     * 初始化日志模块
     *
     * @param context   上下文信息
     */
    public static void init(Context context) {
        if (sHasInitialized) {
            return;
        }

        String namePrefix = getLogNamePrefix(context);
        if (namePrefix == null) return;

        String logPath = getLogPath(context);
        String cachePath = getCachePath(context);

//        if (BuildConfig.DEBUG) {
            Xlog.appenderOpen(Xlog.LEVEL_VERBOSE, Xlog.AppednerModeAsync, cachePath, logPath, namePrefix, "");
            Xlog.setConsoleLogOpen(true);
//        } else {
//            Xlog.appenderOpen(Xlog.LEVEL_INFO, Xlog.AppednerModeAsync, cachePath, logPath, namePrefix, "");
//            Xlog.setConsoleLogOpen(false);
//        }
        Log.setLogImp(new Xlog());

        sHasInitialized = true;
    }

    @Nullable
    public static String getLogNamePrefix(Context context) {
        String processName = null;
        int pid = android.os.Process.myPid();
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
            for (ActivityManager.RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) {
                if (appProcess.pid == pid) {
                    processName = appProcess.processName;
                    break;
                }
            }
        }

        if (!TextUtils.isEmpty(processName)) {
            return !processName.contains(":")
                    ? LOG_FILE_PREFIX
                    : (LOG_FILE_PREFIX + "_" + processName.substring(processName.indexOf(":") + 1));
        }

        return null;
    }

    public static String getLogPath(Context context) {
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
                    context.getPackageName();
            return sdcardPath + "/xlog";
        }
        else {
            return context.getFilesDir() + "/xlog";
        }
    }

    public static String getCachePath(Context context) {
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
                    context.getPackageName();
            return sdcardPath + "/cache/xlog";
        }
        else {
            return context.getCacheDir() + "/xlog";
        }
    }

    public static String getLogFilePath(Context context) {
        return getLogPath(context)
                + "/" + getLogNamePrefix(context) + "_"
                + DateTimeUtils.getCurDateTime("yyyyMMdd")
                + LOG_FILE_POSTFIX;
    }

    /**
     * 反初始化日志模块
     */
    public static void close() {
        Log.appenderClose();
    }
}
